package com.dream.inspect.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.InstructionsEntity;
import com.dream.inspect.modules.business.entity.InstructionsTargetEntity;
import com.dream.inspect.modules.business.service.InstructionsService;
import com.dream.inspect.modules.business.service.InstructionsTargetService;
import com.dream.inspect.modules.dream.pojo.SysDeptFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;
import com.dream.inspect.modules.sys.entity.SysUserEntity;
import com.dream.inspect.modules.sys.service.SysDeptService;



/**
 * 指令表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:56
 */
@RestController
@RequestMapping("business/instructions")
public class InstructionsController extends AbstractController{
    @Autowired
    private InstructionsService instructionsService;
    @Autowired
    private InstructionsTargetService instructionsTargetService;
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:instructions:list")
    public R list(@RequestParam Map<String, Object> params){
    	if(!params.containsKey("fbdwId")){
			SysUserEntity user = getUser();
			params.put("fbdwId", user.getDeptId());
		}
    	if(params.containsKey("titleName")){
    		params.put("titleName", "%" + params.get("titleName") + "%");
    	}
		params.put("deleteFlag", 0);
		params.put("jsyh", getUserId());
        PageUtils page = instructionsService.webQueryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/instructionlist")
    public R instructionlist(@RequestParam Map<String, Object> params){
    	if(!params.containsKey("fbdwId")){
    		SysUserEntity user = getUser();
    		params.put("fbdwId", user.getDeptId());
    	}
    	params.put("deleteFlag", 0);
    	params.put("jsyh", getUserId());
    	PageUtils page = instructionsService.webQueryPage(params);
    	
    	return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    /*@RequiresPermissions("business:instructions:info")*/
    public R info(@PathVariable("id") Long id){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("id", id);
    	params.put("fbdwId", getDeptId());
    	params.put("jsyh", getUserId());
        //InstructionsEntity instructions = instructionsService.selectById(id);
    	PageUtils page = instructionsService.webQueryPage(params);
        return R.ok().put("data", (page.getList().get(0))).put("sessionUser", getSessionUser());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:instructions:save")
    public R save(@RequestBody Map<String, Object> params){
    	String dept = params.get("dept") + "";
		if(StringUtil.isEmpty(dept)){
			return R.error("请选择下达部门").put("data", "请选择下达部门");
		}
		params.put("fbdwId", getDeptId());
		params.put("createUser", getUserId());
		try {
			instructionsService.insertEntity(params);
		} catch (Exception e) {
			logger.error("add instructions error : " + e);
			return R.error("未知异常，请联系管理员").put("data", "未知异常，请联系管理员");
		}
		return R.ok().put("data", "success");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:instructions:update")
    public R update(@RequestBody InstructionsEntity instructions){
        ValidatorUtils.validateEntity(instructions);
        instructionsService.updateAllColumnById(instructions);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:instructions:delete")
    public R delete(@RequestBody Long[] ids){
        instructionsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
	 * 更新指令下达的部门的签收状态
	 * @param target
	 * @return
	 */
	@RequestMapping("/target/update")
	@Transactional
	public R updateTargetEntity(InstructionsTargetEntity target){
		if(null == target.getId()){
			return R.error("请输入指令对象ID").put("data","请输入指令对象ID");
		}
		target.setJszt("1");
		target.setJssj(new Date());
		boolean result = instructionsTargetService.update(target,new EntityWrapper<InstructionsTargetEntity>().eq("id", target.getId()));
		if(result){
			return R.ok().put("data", "success");
		}else{
			return R.error("更新失败").put("data","更新失败");
		}
	}
	
	/**
	 * 获取当前部门非督察条线上级
	 * @param params
	 * @return
	 */
	@RequestMapping("/getParentDept")
	public R getParentDept(@RequestParam Map<String, Object> params){
		params.put("dqdwId", getDeptId());
		params.put("currPage", 0);
		params.put("pageSize", 0);
		List<SysDeptFormMap> list = sysDeptService.findDeptByParentCode(params);
		return R.ok().put("data", list);
	}


	/**
	 * 非督察条线获取下级
	 * @param params
	 * @return
	 */
	@RequestMapping("/getNextNoDept")
	public R getNextNoDept(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqdwId")){
			return R.error("请输入部门ID").put("data", "请输入部门ID");
		}
		params.put("sfdctx","1");
		List<SysDeptFormMap> list = sysDeptService.findNextDeptByParentCode(params);
		return R.ok().put("data", list);
	}
	
	/**
	 * 获取登录用户的信息
	 * @return
	 */
	@RequestMapping("/getUser")
	public R getLoginUser(){
		return R.ok().put("data", getSessionUser());
	}
	
	/**
	 * 获取所有下属部门
	 */
	@RequestMapping("/getDepts")
	public R getDepts(@RequestParam Map<String, Object> params){
		params.put("dqdwId", getDeptId());
		List<SysDeptFormMap> list = sysDeptService.findTreeDept(params);
		return R.ok().put("data", list);
	}

	/**
	 * 获取所有下属部门
	 */
	@RequestMapping("/getDept")
	public R getDept(@RequestParam Map<String, Object> params){
		params.put("dqdwId", getDeptId());
		params.put("flag", "1");
		List<SysDeptFormMap> list = sysDeptService.findTreeDept(params);
		return R.ok().put("data", list);
	}
	
	@RequestMapping("/getAllOtherDept")
	public R getAllOtherDept(){
		Map<String, Object> params = new HashMap<String, Object>();
		SysUserEntity user = getUser();
		params.put("dqdwId",user.getDeptId());
		List<Map<String, Object>> list = sysDeptService.findAllOtherDeptTree(params);
		return R.ok().put("data", list);
	}
	
	@RequestMapping("/target/read")
	public R read(InstructionsTargetEntity target){
		if(null == target.getId()){
			return R.error("请输入指令对象ID").put("data","请输入指令对象ID");
		}
		target.setReadFlag("1");
		boolean result = instructionsTargetService.update(target,new EntityWrapper<InstructionsTargetEntity>().eq("id", target.getId()));
		if(result){
			return R.ok().put("data", "success");
		}else{
			return R.error("更新失败").put("data","更新失败");
		}
	}
}
